//
//  NameViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "NameViewController.h"
#import "NameView.h"
#import "UserInfoModel.h"

@interface NameViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) NameView *nameView;

@end

@implementation NameViewController


- (void)loadView
{
    self.nameView = [[NameView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    
    self.view = self.nameView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor RGBColor:239 green:239 blue:244 alpha:1];
    [self loadNavigationIndfo];
    [self loadNameTextField];
}

#pragma mark - 文本框的一些操作
- (void)loadNameTextField
{
    self.nameView.countLabel.text = [NSString stringWithFormat:@"%lu/15",_userName.length]; // 计算字体的个数
    self.nameView.nameTextField.text = self.userName; //用户昵称
    self.nameView.nameTextField.delegate = self; //设置代理
    [self.nameView.nameTextField addTarget:self action:@selector(countFont) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark - 统计字体个数
- (void)countFont
{
    // 判断 如果字符在20以内 就计算
    if (self.nameView.nameTextField.text.length <= 15)
    {
        self.nameView.countLabel.text = [NSString stringWithFormat:@"%lu/15",_nameView.nameTextField.text.length];
    }
    else // 超过20
    {
        // label 显示20/20
        self.nameView.countLabel.text = @"15/15";
        NSRange rang = NSMakeRange(0, 15);
        // 截取20个字符
        self.nameView.nameTextField.text = [_nameView.nameTextField.text substringWithRange:rang];
    }
}

#pragma mark - 设置导航栏
- (void)loadNavigationIndfo
{
    self.navigationController.navigationBar.barTintColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
    
    self.navigationItem.title = @"昵称";
    // 返回
    UIBarButtonItem *leftBackButton = [UIBarButtonItem itemWithTarget:self Action:@selector(backAction) image:@"home_back" highImage:@"home_back_highlighted"];
    self.navigationItem.leftBarButtonItem = leftBackButton;
    
    // 保存
    UIBarButtonItem *rightSaveButton  = [UIBarButtonItem itemWithTarget:self Action:@selector(saveAction) title:@"保存"];
    self.navigationItem.rightBarButtonItem = rightSaveButton;
    
    // 标题
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
}


#pragma mark - 返回按钮触发事件
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 保存
- (void)saveAction
{
    // 获取用户输入的值
    [self.delegate updateUserName:self.nameView.nameTextField.text];

    [self backAction];
    
}

#pragma mark - 点击空白处键盘回收
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    
    if (![touch.view isKindOfClass: [UITextField class]] || ![touch.view isKindOfClass: [UITextView class]]) {
        
        [self.view endEditing:YES];
        
    }
}

#pragma mark - 内容警告触发方法
- (void)didReceiveMemoryWarning
{
    [[SDImageCache sharedImageCache] clearMemory];
}

@end
